package com.awspaas.user.apps.shhtaerospaceindustrial.controller;

import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

import java.util.List;
import java.util.Map;
/**
 * @author Tao
 * @date 2021年12月10日 09:53
 */
@Controller
public class GetVisitController {
    /**
     * @Description 获取指定时段的保障用车记录-> 入参：sid|开始日期|startdate|结束日期|enddate
     * 出参:部门名称,任务数量、状态（0：成功|1：失败）、信息提示（失败时携带）
     * @date 2021年12月10日 09:53
     */
    @Mapping("com.awspaas.user.apps.shhtaerospaceindustrial_getVisitDeptTotalDate")
    public String getVisitDeptTotalDate(UserContext uc, String sid, String startdate, String enddate) {
        JSONObject returnData = new JSONObject();
        try {
            String querySql = "select t.TARGETDEPT,count(t.id) as missioncount from BO_EU_VISITOR_MANAGE t " +
                    "where t.VISITORNUM>0 and t.isend=1 and t.INPARTTIME between '" + startdate + "' and '" + enddate + "' group by t.TARGETDEPT ";
            System.out.println(querySql);
            List<Map<String, Object>> dataList = DBSql.query(querySql, new ColumnMapRowMapper());
            if (dataList == null || dataList.isEmpty()) {
                returnData.put("status", "0");
                JSONArray DataNull = new JSONArray();
                returnData.put("deptList", DataNull);
                returnData.put("missioncountList", DataNull);
                returnData.put("message", "查询的时段内没有临时用车任务记录");
                return returnData.toString();
            }
            JSONArray deptArr = new JSONArray(); // 部门数组
            JSONArray miscountArr = new JSONArray();// 任务数量数组
            for (Map<String, Object> dataMap : dataList) {
                String applydeptname = CoreUtil.objToStr(dataMap.get("TARGETDEPT"));
                int missioncount = CoreUtil.objToInt(dataMap.get("missioncount"));

                deptArr.add(applydeptname);
                miscountArr.add(missioncount);
            }
            // 成功
            returnData.put("status", "0");
            returnData.put("deptList", deptArr);
            returnData.put("missioncountList", miscountArr);
        } catch (Exception e) {
            e.printStackTrace();
            returnData.put("status", "1");
            returnData.put("message", e.getMessage());
        }
        return returnData.toString();
    }
    /**
     * @Description 获取指定时段的部门用用车记录-> 入参：sid|开始日期|startdate|结束日期|enddate,部门名称|orgname
     * 出参:用车人,用车日期,上下车地点,车辆类型,用车时长,用车里程,用车费用、状态（0：成功|1：失败）、信息提示（失败时携带）
     * @date 2021年12月08日
     */
    @Mapping("com.awspaas.user.apps.shhtaerospaceindustrial_getVisitDeptDetailData")

    public String getVisitDeptDetailData(UserContext uc, String sid, String startdate, String enddate, String orgname) {
        JSONObject returnData = new JSONObject();

        try {
            String userId = uc.getUID();

            String querySql = "select t.TARGETDEPT,t.TARGETMAN,t.INPARTTIME,t.VISITORUNIT,t.VISITORNUM from BO_EU_VISITOR_MANAGE t" +
                    " where t.VISITORNUM>0 and t.isend=1 and t.TARGETDEPT='" + orgname + "' and  t.INPARTTIME between '" + startdate + "' and '" + enddate + "' order by INPARTTIME desc  ";
            System.out.println("==========="+ querySql);
            List<Map<String, Object>> dataList = DBSql.query(querySql, new ColumnMapRowMapper());
            if (dataList == null || dataList.isEmpty()) {//order by INPARTTIME desc
                returnData.put("status", "0");
                JSONArray deptWeekNull = new JSONArray();
                returnData.put("deptWeekList", deptWeekNull);
                returnData.put("message", "时段内未查询到临时用车任务记录");
                return returnData.toString();
            }
            JSONArray deptDetialArr = new JSONArray();
            for (Map<String, Object> dataMap : dataList) {
                JSONObject orderItem = new JSONObject();

                String applyUsername = CoreUtil.objToStr(dataMap.get("TARGETMAN"));
                String visitDate = CoreUtil.objToStr(dataMap.get("INPARTTIME"));
                String visitOrUnit = CoreUtil.objToStr(dataMap.get("VISITORUNIT"));
                String visitOrNum = CoreUtil.objToStr(dataMap.get("VISITORNUM"));

                orderItem.put("userName", applyUsername);
                orderItem.put("visitDate", visitDate);
                orderItem.put("visitOrUnit", visitOrUnit);
                orderItem.put("visitOrNum", visitOrNum);

                deptDetialArr.add(orderItem);
            }

            // 成功
            returnData.put("status", "0");
            returnData.put("deptdataist", deptDetialArr);
        } catch (Exception e) {
            e.printStackTrace();
            returnData.put("status", "1");
            returnData.put("message", e.getMessage());
        }

        return returnData.toString();
    }
    public static Double objToDouble(Object obj) {
        return obj == null ? 0: Double.parseDouble(CoreUtil.objToStr(obj));
    }
}
